/*
 * Problem: 矩阵行交换（Crazy Rows）
 * Author: Yuanshun L
 * Created: 5-Dec-2021
 * Topic: 贪心法
 */
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn = 40+1;
int matrix[maxn][maxn];
int mat[maxn];
int n;

int solve(){
    int res = 0;
    for(int i=0;i<n;i++){
        int j = i;
        for(;j<n;j++)
            if(mat[j]<=i) break;
        for(int k=j-1;k>=i;k--){
            swap(mat[k],mat[k+1]);
            res++;
        }
    }

    return res;
}

int main(){
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);

    int t;
    cin >> n;
    for(int i=0;i<n;i++){
        t = -1;
        for(int j=0;j<n;j++){
            cin >> matrix[i][j];
            if(matrix[i][j] == 1)
                t = j;
        }
        mat[i] = t;
    }

    cout << solve() << endl;

    return 0;
}
